package create;
//继承 Thread, 重写 run
class MyThread1 extends Thread{
    @Override
    public void run() {
        while (true){
            System.out.println("继承Thread,重写run");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
//实现 Runnable, 重写 run
class MyThread2 implements Runnable{
    @Override
    public void run() {
        while (true){
            System.out.println("实现Runnable接口,重写run");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

public class CreateThread {
    public static void main(String[] args) {
        Thread t1 = new MyThread1();
        Runnable runnable = new MyThread2();
        Thread t2 = new Thread(runnable);
        //继承 Thread, 重写 run, 使用匿名内部类
        Thread t3 = new Thread(){
            @Override
            public void run() {
                while (true){
                    System.out.println("继承Thread,重写run,使用匿名内部类");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        //实现 Runnable, 重写 run, 使用匿名内部类
        Thread t4 = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    System.out.println("实现Runnable接口,重写run,使用匿名内部类");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        //使用 lambda 表达式
        Thread t5 = new Thread(()->{
            while (true){
                System.out.println("使用lambda表达式");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        //分别启用每个线程创建的实例
        t1.start();
        t2.start();
        t3.start();
        t4.start();
        t5.start();
        while (true){
            System.out.println("hello main");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
